package com.mdd.common.plugin.sms;

import com.alibaba.fastjson2.JSON;
import com.mdd.common.exception.OperateException;
import com.mdd.common.plugin.sms.engine.EmailSms;
import com.mdd.common.util.ConfigUtils;

import java.util.Map;

public class EmailDriver {
    private final String engine;                // 短信引擎
    private final Map<String, String> config;   // 短信配置
    private String mobile;                      //
    private String subject;
    private String content;
    private Map<String, String> templateParam;  // 短信参数

    /**
     * 构造方法
     */
    public EmailDriver() {
        this.engine = ConfigUtils.get("sms", "engine", "");
        this.config = ConfigUtils.getMap("sms", this.engine);
    }

    /**
     * 设置邮箱
     *
     * @param mobile 邮箱
     * @return SmsDriver
     * @author fzr
     */
    public EmailDriver setMobile(String mobile) {
        this.mobile = mobile;
        return this;
    }

    /**
     * 设置模板编号
     *
     * @param subject 短信编码
     * @return SmsDriver
     * @author fzr
     */
    public EmailDriver setSubject(String subject) {
        this.subject = subject;
        return this;
    }

    public EmailDriver setContent(String content) {
        this.content = content;
        return this;
    }

    /**
     * 设置模板参数
     *
     * @param param 参数
     * @return SmsDriver
     * @author fzr
     */
    public EmailDriver setTemplateParam(Map<String, String> param) {
        this.templateParam = param;
        return this;
    }

    /**
     * 发送短信
     *
     * @author fzr
     */
    public void sendSms() {
        Integer sendResult = 0;
        String results = "";
        EmailSms emailSms = new EmailSms(this.config);
        results = emailSms.setSubject(this.subject)
                .setContent(this.content)
                .setMobile(this.mobile)
                .send();
        sendResult = emailSms.getSendResult();
        if (sendResult == 2) {
            throw new OperateException(results);
        }
    }
}
